//var angular = require('angular');

angular.module('imgApp').controller('UserCtrl', ['$rootScope', '$scope', '$state', '$http', '$timeout', 'localStorageService',
function($rootScope, $scope, $state, $http, $timeout, localStorageService) {
	console.log("=============UserCtrl=============");
	if (localStorageService.get('curUser')) {
		$rootScope.user = localStorageService.get('curUser');
		if ($rootScope.user.role === 0) $rootScope.access = true;
		if ($rootScope.user.password === "qqlogin") {
			$rootScope.isLogin = true;
			$rootScope.normalUser = false;
			$rootScope.qqUser = localStorageService.get('qqUser');
			console.log($rootScope.qqUser);
		} else {
			$rootScope.isLogin = false;
			$rootScope.normalUser = true;
			$rootScope.user = localStorageService.get('curUser');
		}
		if (localStorageService.get('curUserSets')) $rootScope.curSets = localStorageService.get('curUserSets');
		if (localStorageService.get('curUserCollections')) $rootScope.userCollections = new Map(JSON.parse(localStorageService.get('curUserCollections')));
	}
	$rootScope.userCollectionShow = true;
	console.log($rootScope.curSets)
	if ($rootScope.curSets && $rootScope.userCollections) {
		$timeout(function() {
			console.log($rootScope.userCollections);
			for(var i = 0; i < $rootScope.curSets.length; i++) {
				var value = $rootScope.userCollections.get($rootScope.curSets[i].cname);
				if(value) {
					$rootScope.curSets[i].cover = value[0];
				} else {
					$rootScope.curSets[i].cover = {
						img_name: 'nopic',
						cname: ''
					};
				}
			}
		
		}, 1000);
	}

	$scope.backToCollection = function() {
		$rootScope.userCollectionShow = true;
	}
	$scope.user = $rootScope.user;
	
	$scope.addCollection = function() {
		$("#new-collection").modal('show');
	}
	$scope.backToUserCollection = function() {
		$rootScope.userCollectionShow = true;
	}

	$scope.delCollection = function($event, cname, index) {
		$event.stopPropagation();
		var verify = confirm("确认删除该图集么？");
		if (verify) {
			$http({
				method: 'DELETE',
				url: $rootScope.baseUrl + 'api/collections/' + cname
			}).then(function(res) {
				console.log(res.data);
				$rootScope.getUserCollections(true);
				$timeout(function() {
					for(var i = 0; i < $rootScope.curSets.length; i++) {
						var value = $rootScope.userCollections.get($rootScope.curSets[i].cname);
						if(value) {
							$rootScope.curSets[i].cover = value[0];
						} else {
							$rootScope.curSets[i].cover = {
								img_name: 'nopic',
								cname: ''
							};
						}
					}
					$scope.$apply(function() {
						$scope.userSets = $rootScope.curSets;
						console.log($scope.userSets);
					})
				}, 1000);
				alert(res.data);
				
			}, function(err) {
				console.log(err.data);
			});
		}
	}
	
	$scope.createNewCollection = function() {
		$scope.type = $scope.type || "not set";
		console.log($scope.user);
		$http({
			method:'POST',
			url: $rootScope.baseUrl + 'api/collections',
			data: {
				id: $scope.user.id,
				cname: $scope.newCollectionName,
				type: $scope.type
			}
		}).then(function(res) {
			console.log(res.data);
			alert(res.data);
			$("#new-collection").modal('hide');
			$scope.newCollectionName = '';
			$scope.type = '';
			$rootScope.getUserCollections(true);
			$timeout(function() {
				for(var i = 0; i < $rootScope.curSets.length; i++) {
					var value = $rootScope.userCollections.get($rootScope.curSets[i].cname);
					if(value) {
						$rootScope.curSets[i].cover = value[0];
					} else {
						$rootScope.curSets[i].cover = {
							img_name: 'nopic',
							cname: ''
						};
					}
				}
				$scope.$apply(function() {
					$scope.userSets = $rootScope.curSets;
					console.log($scope.userSets);
				})
			}, 1000);
		}, function(err) {
			console.log(err.data);
		});
	}
	
	$scope.deleteImg = function($event, img) {
		$event.stopPropagation();
		console.log(img);
		//$scope.unbind(img);
	}
	$scope.unbind = function(img) {
		var index = -1,
			arr = [];
		for (var i = 0; i < $rootScope.userImgs.length; i++) {
			if (img.id === $rootScope.userImgs[i].id) {
				index = i;
			}
			arr.push($rootScope.userImgs[i].id);
		}
		if (index >= 0) {
			$rootScope.userImgs.splice(index, 1);
			arr.splice(index, 1);
			var sid = arr.toString();
			$http({
				method: "POST",
				url:   $rootScope.baseUrl + "api/images/bind",
				data: {
					iname: img.img_name,
			    	cname: $rootScope.userCollectionName,
			    	sid: sid
				}
			}).then(function(res) {
				
			}, function(err) {
				
			});
			$("#img-view").modal('hide');
		}
	}
	
	$scope.viewImg = function(img) {
		console.log($rootScope.curSetIndex);
		$scope.curImg = img;
		var pic = document.getElementById("userimg-open");
		pic.onload = function() {
			var w = pic.naturalWidth,
				h = pic.naturalHeight;
			autofit(pic, w, h, d);
		}
		$("#img-view").modal('show');
	}
	$scope.close = function() {
		$("#img-view").modal('hide');
	}
	
	$scope.toggleImg = function(img) {
		$scope.curImg = img;
	}
	
	$('.user-area-nav').hover(function() {
		$('.user-options').removeClass('ng-hide');
	});
	
	$('.user-options').mouseout(function() {
		$timeout(function() {
			$('.user-options').addClass('ng-hide');
		}, 1000);
	});
	$('.user-options').hover(function() {
		$('.user-options').removeClass('ng-hide');
	});
	
	$rootScope.logOut = function() {
		$state.go('home');
		if(QC.Login.check()) {
			QC.Login.signOut();
			if(!QC.Login.check()) {
				console.log("已经登出!");
			}
		}
		$rootScope.isLogin = false;
		$rootScope.normalUser = false;
		$rootScope.user = {};
		$rootScope.qqUser = {};
		$rootScope.access = false;
	}
}]);